/**
 * 
 */
package demo.tellurium.module.cqp

import org.telluriumsource.ui.object.InputBox;

import demo.tellurium.framework.BaseUIModule;

/**
 * @author roger.xu
 *
 */
class CQPAnswerModule extends BaseUIModule {

    private static final String MODULE_NAME = "CQPAnswer";
    public static final String PATH_KEY = "cqp.answer.path";

    @Override
    public String getModuleName() {
        return MODULE_NAME;
    }

    @Override
    public void defineUi() {
        ui.Container(uid: "CQPAnswer") {
            // Toolbar
            Container(uid: "Toolbar", clocator: [id: "toolbar"]) {
                DOJO_Button(uid: "SelectAll", clocator: [tag: "span", widgetid: "selectOrDeselectAllLink"])
                DOJO_Button(uid: "AnswerButton", clocator: [tag: "span", widgetid: "answerButton"])
                DOJO_Button(uid: "ResetButton", clocator: [tag: "span", widgetid: "resetButton"])
                DOJO_Button(uid: "ReportButton", clocator: [tag: "span", widgetid: "reportButton"])
                DOJO_Button(uid: "FileUploadButton", clocator: [tag: "span", widgetid: "fileUploadButton"])
                DOJO_Button(uid: "STGCriteriaButton", clocator: [tag: "span", widgetid: "selectCriteriaButton"])
                DOJO_Button(uid: "ToggleTargetScoreButton", clocator: [tag: "span", widgetid: "targetScoreButton"])
            }
            // CriteriaCells
            List(uid: "QuestionGroups", clocator: [tag: "div", id: "criteriaCells"], separator: "div[class*='dijitContentPane']:visible") {
                Container(uid: "{all}") {
                    Container(uid: "QuestionGroupTitle", clocator: [tag: "div", id: "^QGTitle_"]) {
                        Span(uid: "SelectAllCheckBox", clocator: [id: "^GroupButton_"])
                        Span(uid: "TitleText", clocator: [id: "^QGTitleHref_"])
                    }
                    List(uid: "Questions", clocator: [tag: "div", id: "^QG_"], separator: "div:visible") {
                        Container(uid: "{all}") {
                            DOJO_CheckBox(uid: "CheckBox")
                            Container(uid: "Title", clocator: [tag: "span"]) {
                                Span(uid: "Label", clocator: [tag: "label"])
                            }
                            UrlLink(uid: "Link", clocator: [tag: "a"])
                        }
                    }
                }
            }
            // AnswerQuestionPopup
            Container(uid: "AnswerQuestionPopup", clocator: [tag: "div", id: "AnswerQuestionPopup"]) {
                UrlLink(uid: "FoldLink", clocator: [id: "collapseLink"])
                Container(uid: "ButtonBar_Top", clocator: [class: "top-button-bar"]) {
                    Button(uid: "SaveButton", clocator: [tag: "input", type: "button", class: "SaveButton"])
                    Button(uid: "CloseButton", clocator: [tag: "input", type: "button", class: "CloseButton"])
                }
                List(uid: "CriteriasContent", clocator: [tag: "div", id: "criteriasContent"], separator: "table[class='consumability_assessment_layer_internals']") {
                    Container(uid: "{all}", clocator: [tag: "table", id: "^criteriaMain_"]) {
                        Table(uid: "Table", clocator: [tag: "table"]) {
                            // header
                            TextBox(uid: "{header: all}", clocator: [:])
                            // first cell
                            TextBox(uid: "{row: 1, column: 1}", clocator: [:])
                            // radio button column
                            Container(uid: "{row: all, column: 1}", clocator: [:]) {
                                DOJO_RadioButton(uid: "RadioButton")
                            }
                            // description column
                            TextBox(uid: "{row: all, column: 2}", clocator: [:])
                        }
                        InputBox(uid: "CommentTextArea",  clocator: [tag: "textarea", class: "dijitTextArea", name: "comment"])
                    }
                }
                Container(uid: "ButtonBar_Bottom", clocator: [class: "bottom-button-bar"]) {
                    Button(uid: "SaveButton", clocator: [tag: "input", type: "button", class: "SaveButton"])
                    Button(uid: "CloseButton", clocator: [tag: "input", type: "button", class: "CloseButton"])
                }
            }
            // Info Dialog
            Container(uid: "InfoDialog", clocator: [id: "infoDlg"]) {
                Button(uid: "OKButton", clocator: [tag: "input", type: "button", id: "infoDlg_OK_btn"])
            }
            // ResetDialog
            Container(uid: "ResetDialog", clocator: [id: "confirmListDlg"]) {
                Button(uid: "OKButton", clocator: [tag: "input", type: "button", id: "confirmDlgButtonSubmit"])
                Button(uid: "CancelButton", clocator: [tag: "input", type: "button", id: "confirmDlgButtonCancel"])
            }
            // FileUploadDialog
            Container(uid: "FileUploadDialog", clocator: [id: "otherSupportingInfoWindow"]) {
                Container(uid: "ButtonBar", clocator: [tag: "div", class: "buttonbar"]) {
                    Button(uid: "SaveButton", clocator: [tag: "input", type: "button", class: "SaveButton"])
                    Button(uid: "CloseButton", clocator: [tag: "input", type: "button", class: "CloseButton"])
                }
            }

        }
    }

    /* ---- Toolbar -------*/
    public void doClickSelectAllButton() {
        def toolbarUid = String.format("%s.Toolbar", getModuleName());
        def widgetUid = String.format("%s.%s", toolbarUid, "SelectAll");
        doClickButton(widgetUid);
    }

    public void doClickAnswerButton() {
        def toolbarUid = String.format("%s.Toolbar", getModuleName());
        def widgetUid = String.format("%s.%s", toolbarUid, "AnswerButton");
        doClickButton(widgetUid);
    }

    public void doClickResetButton() {
        def toolbarUid = String.format("%s.Toolbar", getModuleName());
        def widgetUid = String.format("%s.%s", toolbarUid, "ResetButton");
        doClickButton(widgetUid);
    }

    public void doClickReportButton() {
        def toolbarUid = String.format("%s.Toolbar", getModuleName());
        def widgetUid = String.format("%s.%s", toolbarUid, "ReportButton");
        doClickButton(widgetUid);
    }

    public void doClickFileUploadButton() {
        def toolbarUid = String.format("%s.Toolbar", getModuleName());
        def widgetUid = String.format("%s.%s", toolbarUid, "FileUploadButton");
        doClickButton(widgetUid);
    }

    public void doClickSTGCriteriaButton() {
        def toolbarUid = String.format("%s.Toolbar", getModuleName());
        def widgetUid = String.format("%s.%s", toolbarUid, "STGCriteriaButton");
        doClickButton(widgetUid);
    }

    public void doClickToggleTargetScoreButton() {
        def toolbarUid = String.format("%s.Toolbar", getModuleName());
        def widgetUid = String.format("%s.%s", toolbarUid, "ToggleTargetScoreButton");
        doClickButton(widgetUid);
    }

    /* ---- Answer Question -------*/
    public String doGetQuestionGroupTitle(int index) {
        def listUid = String.format("%s.QuestionGroups", getModuleName());
        def titleUid = String.format("%s[%d].QuestionGroupTitle.TitleText", listUid, index);

        String cssSelector = getSelector(titleUid);
        int number = getCssSelectorCount(cssSelector);
        console.debug("[cssSelector]", cssSelector, number);

        return getText(titleUid);
    }

    public void doClickQuestionGroupSelectAllCheckBox(int index) {
        // click option list
        def listUid = String.format("%s.QuestionGroups", getModuleName());
        def options = getVisibleOptionUids(listUid, "QuestionGroupTitle.SelectAllCheckBox", true);
        clickListOption(options, index);
    }

    public void doClickQuestionCheckBox(int questionGroupIndex, int questionIndex) {
        def listUid = String.format("%s.QuestionGroups[%d].Questions", getModuleName(), questionGroupIndex);
        int listSize = getListSize(listUid);

        console.debug(listUid, questionIndex, "/", listSize);
        def checkboxUid = String.format("%s[%d].CheckBox", listUid, questionIndex);
        console.debug(getLocator(checkboxUid));

        doClickCheckBox(checkboxUid);
    }

    public void doClickQuestionLink(int questionGroupIndex, int questionIndex) {
        def listUid = String.format("%s.QuestionGroups[%d].Questions", getModuleName(), questionGroupIndex);
        def linkUid = String.format("%s[%d].Link",listUid, questionIndex);
        click linkUid
        pause 500
    }

    public void doClickFoldLink() {
        def popupUid = String.format("%s.AnswerQuestionPopup", getModuleName());
        def foldLinkUid = String.format("%s.%s", popupUid, "FoldLink");
        click foldLinkUid
        pause 500
    }

    public void doClickQuestionRadioButton(int optionIndex) {
        def rowIndex = optionIndex + 1;
        def columnIndex = 1;
        def tableUid = String.format("%s.AnswerQuestionPopup.CriteriasContent[1].Table", getModuleName());
        def optionUid = String.format("%s[%d][%d].RadioButton", tableUid, rowIndex, columnIndex);
        doClickRadioButton(optionUid);
    }

    public void doInputQuestionComment(String input) {
        def textareaUid = String.format("%s.AnswerQuestionPopup.CriteriasContent[1].CommentTextArea", getModuleName());
        type textareaUid, input
        pause 500
    }

    public void doClickAnswerDialogSaveButton() {
        def popupUid = String.format("%s.AnswerQuestionPopup", getModuleName());
        def buttonUid = String.format("%s.ButtonBar_Bottom.%s", popupUid, "SaveButton");
        click buttonUid
        pause 2000
    }

    public void doClickAnswerDialogCloseButton() {
        def popupUid = String.format("%s.AnswerQuestionPopup", getModuleName());
        def buttonUid = String.format("%s.ButtonBar_Bottom.%s", popupUid, "CloseButton");
        click buttonUid
        pause 500
    }

    /* ---- Info Dialog ---------------*/
    public void doClickInfoDialogOKButton() {
        def popupUid = String.format("%s.InfoDialog", getModuleName());
        def buttonUid = String.format("%s.%s", popupUid, "OKButton");
        click buttonUid
        pause 500
    }

    /* ---- ResetDialog -------*/
    public void doClickResetDialogOKButton() {
        def popupUid = String.format("%s.ResetDialog", getModuleName());
        def buttonUid = String.format("%s.%s", popupUid, "OKButton");
        click buttonUid
        pause 2000
    }

    public void doClickResetDialogCancelButton() {
        def popupUid = String.format("%s.ResetDialog", getModuleName());
        def buttonUid = String.format("%s.%s", popupUid, "CancelButton");
        click buttonUid
        pause 500
    }

    /* ---- FileUploadDialog -------*/
    public void doClickFileUploadDialogSaveButton() {
        def popupUid = String.format("%s.FileUploadDialog", getModuleName());
        def buttonUid = String.format("%s.ButtonBar.%s", popupUid, "SaveButton");
        click buttonUid
        pause 2000
    }

    public void doClickFileUploadDialogCloseButton() {
        def popupUid = String.format("%s.FileUploadDialog", getModuleName());
        def buttonUid = String.format("%s.ButtonBar.%s", popupUid, "CloseButton");
        click buttonUid
        pause 500
    }

    /* ---- Bundle Operation -------*/
    public void doAnswerQuestion() {
        doClickQuestionLink(1, 1);
        doClickQuestionRadioButton(1);
        doInputQuestionComment("comment");
        doClickAnswerDialogSaveButton();
        doClickAnswerDialogCloseButton();
    }

    public void doAnswerAll() {
        def optionIndex = 1;
        def rowIndex = optionIndex + 1;
        def columnIndex = 1;

        def listUid = String.format("%s.AnswerQuestionPopup.CriteriasContent", getModuleName());
        def listSize = getListSize(listUid);
        for (i in 1..listSize) {
            def tableUid = String.format("%s[%d].Table", listUid, i);
            def optionUid = String.format("%s[%d][%d].dijitRadio.RadioButton", tableUid, rowIndex, columnIndex);
            click optionUid
            pause 100
        }
    }
}
